// datetime组件实现
window.add_datetime = function(x, y, w, h, type, format, hide, tag, callback, divid) {
    // 确保参数有默认值
    x = x || 100;
    y = y || 100;
    w = w || 200;
    h = h || 40;
    type = type || 1; // 1=日期, 2=时间, 3=日期时间
    format = format || (type === 1 ? 'YYYY-MM-DD' : type === 2 ? 'HH:mm:ss' : 'YYYY-MM-DD HH:mm:ss');
    hide = hide || false;
    tag = tag || '';
    callback = callback || null;
    
    // 生成唯一ID
    const id = 'datetime_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
    
    // 创建输入元素
    const input = document.createElement('input');
    input.id = id;
    input.type = 'text';
    input.readOnly = true;
    
    // 设置样式
    input.style.position = 'absolute';
    input.style.left = x + 'px';
    input.style.top = y + 'px';
    input.style.width = w + 'px';
    input.style.height = h + 'px';
    input.style.padding = '0 10px';
    input.style.border = '1px solid #e6e6e6';
    input.style.borderRadius = '2px';
    input.style.fontSize = '14px';
    
    // 设置初始值
    const now = new Date();
    let value = '';
    
    if (type === 1) {
        value = now.toISOString().split('T')[0];
    } else if (type === 2) {
        value = now.toTimeString().split(' ')[0];
    } else {
        value = now.toISOString().replace('T', ' ').split('.')[0];
    }
    
    input.value = value;
    
    // 附加样式和标记
    if (hide) input.style.display = 'none';
    if (tag) input.setAttribute('data-tag', tag);
    
    // 事件处理
    input.onmousemove = function(e) { handleEvent(id, 0, input.value, tag, e.clientX, e.clientY, null, callback, divid); };
    input.onclick = function(e) { 
        // 点击时弹出日期时间选择器
        layui.laydate.render({
            elem: input,
            type: type === 1 ? 'date' : type === 2 ? 'time' : 'datetime',
            format: format,
            done: function(value) {
                handleEvent(id, 1, value, tag, 0, 0, null, callback, divid);
            }
        });
        handleEvent(id, 1, input.value, tag, e.clientX, e.clientY, null, callback, divid); 
    };
    input.oncontextmenu = function(e) { e.preventDefault(); handleEvent(id, 2, input.value, tag, e.clientX, e.clientY, null, callback, divid); return false; };
    input.onmouseover = function(e) { handleEvent(id, 5, input.value, tag, e.clientX, e.clientY, null, callback, divid); };
    input.onmouseout = function(e) { handleEvent(id, 6, input.value, tag, e.clientX, e.clientY, null, callback, divid); };
    input.onkeydown = function(e) { handleEvent(id, 3, input.value, tag, 0, 0, e.key, callback, divid); };
    input.onkeyup = function(e) { handleEvent(id, 4, input.value, tag, 0, 0, e.key, callback, divid); };
    
    // 添加到页面或指定父div
    const parent = divid ? document.getElementById(divid) : document.body;
    parent.appendChild(input);
    
    // 初始化datetimes存储空间
    if (!elementStore.datetimes) {
        elementStore.datetimes = {};
    }
    
    // 存储引用
    elementStore.datetimes[id] = input;
    
    return id;
}

window.del_datetime = function(id) {
    if (elementStore.datetimes[id]) {
        elementStore.datetimes[id].remove();
        delete elementStore.datetimes[id];
    }
}

window.upd_datetime = function(id, x, y, w, h, type, format, hide) {
    const datetime = elementStore.datetimes[id];
    if (datetime) {
        if (x !== undefined) datetime.style.left = x + 'px';
        if (y !== undefined) datetime.style.top = y + 'px';
        if (w !== undefined) datetime.style.width = w + 'px';
        if (h !== undefined) datetime.style.height = h + 'px';
        if (type !== undefined || format !== undefined) {
            // 更新日期时间格式
            const now = new Date();
            let value = '';
            
            if (type === 1) {
                value = now.toISOString().split('T')[0];
            } else if (type === 2) {
                value = now.toTimeString().split(' ')[0];
            } else {
                value = now.toISOString().replace('T', ' ').split('.')[0];
            }
            
            datetime.value = value;
        }
        if (hide !== undefined) datetime.style.display = hide ? 'none' : 'block';
    }
}